package cas;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

class  ClickNumber{
    int number=0;

    //(1). 使用synchronized实现number++
    public synchronized void add_synchronized(){
        number++;
    }
    //(2). 使用AtomicInteger
    AtomicInteger atomicInteger=new AtomicInteger();
    public void add_atomicInteger(){
        atomicInteger.incrementAndGet();
    }
   //(3). 使用AtomicLong
   AtomicLong atomicLong=new AtomicLong();
   public void add_atomicLong(){
       atomicLong.incrementAndGet();
   }
   //(4). 使用LongAdder
   LongAdder adder=new LongAdder();
   public void add_longAdder(){
       adder.increment();
   }
   //(5). 使用LongAccumulater
   LongAccumulator accumulator=new LongAccumulator((x, y)->x+y,0);
   public void add_longAccumulater(){
       accumulator.accumulate(1);
   }
}
/**
 * 50个线程,每个线程100w次,总点赞数出来
 * */
public class LongAdderCalcDemo {
    // 50个线程和每个线程点在100w次
    public static  final int SIZE_THREAD=50;
    public static  final int _1w=10000;
    public static void main(String[] args) throws Exception {
//        CountDownLatchDemo();

//        CyclicBarrier cyclicBarrier = new CyclicBarrier(SIZE_THREAD);
//        ClickNumber clickNumber=new ClickNumber();
//        long startTime = System.currentTimeMillis();
//        for (int i = 1 ; i <=SIZE_THREAD ; i++) {
//            new Thread(()->{
//                try{
//                    for (int j = 1; j <=10*_1w; j++) {
//                        //我们明显可以看到调用LongAdder性能最好
//                        //clickNumber.add_synchronized();
//                        clickNumber.add_longAdder();
//                    }
//                }catch (Exception e){
//                }finally {
//                    try {
//                        cyclicBarrier.await();
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    } catch (BrokenBarrierException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//            },String.valueOf(i)).start();
//        }
//        long endTime = System.currentTimeMillis();
//        System.out.println("-----consTime:"+(endTime-startTime)+"毫秒"+"\t");
//        System.out.println(clickNumber.adder.longValue());

    }

    private static void CountDownLatchDemo() throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(SIZE_THREAD);
        ClickNumber clickNumber=new ClickNumber();
        long startTime = System.currentTimeMillis();
        for (int i = 1 ; i <=SIZE_THREAD ; i++) {
            new Thread(()->{
                try{
                    for (int j = 1; j <=10*_1w; j++) {
                        //我们明显可以看到调用LongAdder性能最好
                        //clickNumber.add_synchronized();
                        clickNumber.add_longAdder();
                    }
                }finally {
                    countDownLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        long endTime = System.currentTimeMillis();
        System.out.println("-----consTime:"+(endTime-startTime)+"毫秒"+"\t");
        System.out.println(clickNumber.adder.longValue());
    }
}
